<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="站点名称" prop="transplantStation">
        <el-input
          v-model="queryParams.transplantStation"
          placeholder="请输入站点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="村屯名称" prop="transplantVillage">
        <el-input
          v-model="queryParams.transplantVillage"
          placeholder="请输入村屯名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="农户名称" prop="transplantFarmer">
        <el-input
          v-model="queryParams.transplantFarmer"
          placeholder="请输入农户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:transplant:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:transplant:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:transplant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:transplant:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="transplantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="站点名称" align="center" prop="transplantStation" />
      <el-table-column label="村屯名称" align="center" prop="transplantVillage" />
      <el-table-column label="农户名称" align="center" prop="transplantFarmer" />
      <el-table-column label="种植面积" align="center" prop="transplantPlantingArea" />
      <el-table-column label="移栽时间" align="center" prop="transplantTransplantingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.transplantTransplantingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="移栽方式" align="center" prop="transplantTransplantingMethod" />
      <el-table-column label="是否盖膜" align="center" prop="transplantIsMulched">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.transplantIsMulched"/>
        </template>
      </el-table-column>
      <el-table-column label="行距测量值" align="center" prop="transplantRowSpacingMeasurement" />
      <el-table-column label="行距平均值" align="center" prop="transplantRowSpacingAverage" />
      <el-table-column label="株距测量值" align="center" prop="transplantPlantSpacingMeasurement" />
      <el-table-column label="株距平均值" align="center" prop="transplantPlantSpacingAverage" />
      <el-table-column label="基肥施肥时间" align="center" prop="transplantBaseFertilizerTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.transplantBaseFertilizerTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="基肥施肥方式" align="center" prop="transplantBaseFertilizerMethod" />
      <el-table-column label="基肥施肥种类" align="center" prop="transplantBaseFertilizerType" />
      <el-table-column label="基肥施用量" align="center" prop="transplantBaseFertilizerAmount" />
      <el-table-column label="定根肥施肥种类" align="center" prop="transplantRootFixingFertilizerType" />
      <el-table-column label="定根肥施用量" align="center" prop="transplantRootFixingFertilizerAmount" />
      <el-table-column label="提苗肥施肥时间" align="center" prop="transplantSeedlingFertilizerTime" />
      <el-table-column label="提苗肥施肥种类" align="center" prop="transplantSeedlingFertilizerType" />
      <el-table-column label="提苗肥施用量" align="center" prop="transplantSeedlingFertilizerAmount" />
      <el-table-column label="钼肥施用量" align="center" prop="transplantMolybdenumFertilizerAmount" />
      <el-table-column label="病虫害情况" align="center" prop="transplantPestDiseases" />
      <el-table-column label="备注信息" align="transplantRemarks">
        <template slot-scope="scope">
          <el-tooltip effect="dark" :content="scope.row.transplantRemarks" placement="top">
            <span>{{ scope.row.transplantRemarks.slice(0, 15) }}{{ scope.row.transplantRemarks.length > 15? '...' : '' }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:transplant:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:transplant:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改烟田移栽情况对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="站点名称" prop="transplantStation">
          <el-input v-model="form.transplantStation" placeholder="请输入站点名称" />
        </el-form-item>
        <el-form-item label="村屯名称" prop="transplantVillage">
          <el-input v-model="form.transplantVillage" placeholder="请输入村屯名称" />
        </el-form-item>
        <el-form-item label="农户名称" prop="transplantFarmer">
          <el-input v-model="form.transplantFarmer" placeholder="请输入农户名称" />
        </el-form-item>
        <el-form-item label="种植面积" prop="transplantPlantingArea">
          <el-input v-model="form.transplantPlantingArea" placeholder="请输入种植面积" />
        </el-form-item>
        <el-form-item label="移栽时间" prop="transplantTransplantingTime">
          <el-date-picker clearable
            v-model="form.transplantTransplantingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择移栽时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="移栽方式" prop="transplantTransplantingMethod">
          <el-input v-model="form.transplantTransplantingMethod" placeholder="请输入移栽方式" />
        </el-form-item>
        <el-form-item label="是否盖膜" prop="transplantIsMulched">
          <el-select v-model="form.transplantIsMulched" placeholder="请选择是否盖膜">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="行距测量值" prop="transplantRowSpacingMeasurement">
          <el-input v-model="form.transplantRowSpacingMeasurement" placeholder="请输入行距测量值" />
        </el-form-item>
        <el-form-item label="行距平均值" prop="transplantRowSpacingAverage">
          <el-input v-model="form.transplantRowSpacingAverage" placeholder="请输入行距平均值" />
        </el-form-item>
        <el-form-item label="株距测量值" prop="transplantPlantSpacingMeasurement">
          <el-input v-model="form.transplantPlantSpacingMeasurement" placeholder="请输入株距测量值" />
        </el-form-item>
        <el-form-item label="株距平均值" prop="transplantPlantSpacingAverage">
          <el-input v-model="form.transplantPlantSpacingAverage" placeholder="请输入株距平均值" />
        </el-form-item>
        <el-form-item label="基肥施肥时间" prop="transplantBaseFertilizerTime">
          <el-date-picker clearable
            v-model="form.transplantBaseFertilizerTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择基肥施肥时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="基肥施肥方式" prop="transplantBaseFertilizerMethod">
          <el-input v-model="form.transplantBaseFertilizerMethod" placeholder="请输入基肥施肥方式" />
        </el-form-item>
        <el-form-item label="基肥施用量" prop="transplantBaseFertilizerAmount">
          <el-input v-model="form.transplantBaseFertilizerAmount" placeholder="请输入基肥施用量" />
        </el-form-item>
        <el-form-item label="定根肥施用量" prop="transplantRootFixingFertilizerAmount">
          <el-input v-model="form.transplantRootFixingFertilizerAmount" placeholder="请输入定根肥施用量" />
        </el-form-item>
        <el-form-item label="提苗肥施肥时间" prop="transplantSeedlingFertilizerTime">
          <el-input v-model="form.transplantSeedlingFertilizerTime" placeholder="请输入提苗肥施肥时间" />
        </el-form-item>
        <el-form-item label="提苗肥施用量" prop="transplantSeedlingFertilizerAmount">
          <el-input v-model="form.transplantSeedlingFertilizerAmount" placeholder="请输入提苗肥施用量" />
        </el-form-item>
        <el-form-item label="钼肥施用量" prop="transplantMolybdenumFertilizerAmount">
          <el-input v-model="form.transplantMolybdenumFertilizerAmount" placeholder="请输入钼肥施用量" />
        </el-form-item>
        <el-form-item label="病虫害情况" prop="transplantPestDiseases">
          <el-input v-model="form.transplantPestDiseases" placeholder="请输入病虫害情况" />
        </el-form-item>
        <el-form-item label="备注信息" prop="transplantRemarks">
          <el-input v-model="form.transplantRemarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTransplant, getTransplant, delTransplant, addTransplant, updateTransplant } from "@/api/system/transplant";

export default {
  name: "Transplant",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 烟田移栽情况表格数据
      transplantList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        transplantStation: null,
        transplantVillage: null,
        transplantFarmer: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        transplantStation: [
          { required: true, message: "站点名称不能为空", trigger: "blur" }
        ],
        transplantVillage: [
          { required: true, message: "村屯名称不能为空", trigger: "blur" }
        ],
        transplantFarmer: [
          { required: true, message: "农户名称不能为空", trigger: "blur" }
        ],
        transplantPlantingArea: [
          { required: true, message: "种植面积不能为空", trigger: "blur" }
        ],
        transplantTransplantingTime: [
          { required: true, message: "移栽时间不能为空", trigger: "blur" }
        ],
        transplantTransplantingMethod: [
          { required: true, message: "移栽方式不能为空", trigger: "blur" }
        ],
        transplantIsMulched: [
          { required: true, message: "是否盖膜不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询烟田移栽情况列表 */
    getList() {
      this.loading = true;
      listTransplant(this.queryParams).then(response => {
        this.transplantList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        transplantStation: null,
        transplantVillage: null,
        transplantFarmer: null,
        transplantPlantingArea: null,
        transplantTransplantingTime: null,
        transplantTransplantingMethod: null,
        transplantIsMulched: null,
        transplantRowSpacingMeasurement: null,
        transplantRowSpacingAverage: null,
        transplantPlantSpacingMeasurement: null,
        transplantPlantSpacingAverage: null,
        transplantBaseFertilizerTime: null,
        transplantBaseFertilizerMethod: null,
        transplantBaseFertilizerType: null,
        transplantBaseFertilizerAmount: null,
        transplantRootFixingFertilizerType: null,
        transplantRootFixingFertilizerAmount: null,
        transplantSeedlingFertilizerTime: null,
        transplantSeedlingFertilizerType: null,
        transplantSeedlingFertilizerAmount: null,
        transplantMolybdenumFertilizerAmount: null,
        transplantPestDiseases: null,
        transplantRemarks: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加烟田移栽情况";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTransplant(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改烟田移栽情况";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTransplant(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTransplant(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除烟田移栽情况编号为"' + ids + '"的数据项？').then(function() {
        return delTransplant(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/transplant/export', {
        ...this.queryParams
      }, `transplant_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
